package com.cn.ccp.action.course;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.CourseEvaluationBean;
import com.cn.ccp.bean.FilePathBean;
import com.cn.ccp.util.CCPContants;

/**
 * 课程action
 * @author renyongcheng
 *
 */
public class CourseEvaluationAction extends BaseAction{
	private Logger log = Logger.getLogger(CourseEvaluationAction.class);
	private String courseId,courseName ,evaluationContent;//课程id
	private CourseEvaluationBean outline;
	public CourseEvaluationAction(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		if(varMap.get("id") != null){
			courseId = varMap.get("id");
			log.info("----------courseId = "+courseId);
			Object obj = getCourseService().getEvaluationByCourseId(courseId);
			if(obj != null)
				outline = (CourseEvaluationBean)obj;
			CourseBean cb = getCourseService().getBeanById(courseId);
			courseName = cb.getCourse_name();
		}
	}
	
	/**
	 * 保存公告
	 * @return
	 */
	public String commint(){
		
		Object contents = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("content1");
		log.info("-------------------contents = "+contents);
		log.info("-------------------courseId = "+courseId);
		FilePathBean path = getFilePathService().getFlagBean();
		String savePath = CCPContants.SYS_PATH+"files"+File.separator;
		if(path != null){
			savePath = path.getPath()  ;
		}
		if(outline.getId() != 0){//更新大纲文件内容
//			getCourseService().updateCourseOutline(outline.getId(), contents.toString());
			log.info(savePath);
			if(!outline.getContents().endsWith(".txt")){
				String uuid = UUID.randomUUID().toString();
				String fileName = CCPContants.getCurrUserId()  + "/" +  uuid + ".txt";
				savePath +=  fileName;
				log.info(savePath);
//				getCourseService().saveCourseEvaluation(outline.getId(),fileName, path.getId());
				getCourseService().saveCourseEvaluation(courseId, path.getId(),fileName);
			}else
				savePath += outline.getContents();
		}else{	
			//将大纲内容写入txt文件
			String uuid = UUID.randomUUID().toString();
			String fileName = CCPContants.getCurrUserId() +"/"+uuid + ".txt";
			savePath +=  fileName;
			log.info(savePath);
			getCourseService().saveCourseEvaluation(courseId, path.getId(),fileName);
		}
		FileWriter fw = null;
		try {
			File file = new File(savePath);
			if(file.exists())
				file.deleteOnExit();
			file.createNewFile();
			fw = new FileWriter(savePath);
			fw.write(contents.toString(),0,contents.toString().length());    
			fw.flush();    
		} catch (IOException e) {
			log.error("save outline failed : ", e);
		} finally{
			try {
				if(fw != null)
					fw.close();
			} catch (IOException e) {
			}
		}   
		
		return MAIN;
	}
	
	public String back(){
		return MAIN;
	}


	public String getCourseId() {
		return courseId;
	}


	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public CourseEvaluationBean getOutline() {
		if(outline == null)
			outline = new CourseEvaluationBean();
		return outline;
	}

	public void setOutline(CourseEvaluationBean outline) {
		this.outline = outline;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	private String getOutlineFilePath(){
		FilePathBean path = getFilePathService().getFlagBean();
		String savePath = CCPContants.SYS_PATH+"files"+File.separator ;
		if(path != null){
			savePath = path.getPath() ;
		}
		return savePath;
	}
	public String getEvaluationContent() {
		evaluationContent = null; 
		if(outline != null){
			String savePath = getOutlineFilePath();
			if(outline.getContents() != null && outline.getContents().endsWith(".txt")){
				savePath += outline.getContents();
				log.info(savePath);
				BufferedReader reader = null;
				StringBuffer buf = new StringBuffer();
				try {
					reader = new BufferedReader(new FileReader(savePath));
					String text = null;
					while ((text = reader.readLine()) != null){
						 buf.append(text);
						 buf.append(System.getProperty("line.separator"));
					}
					evaluationContent = buf.toString();
				} catch (IOException e) {
					log.info("read outline failed : ",e);
				} finally{
					try {
						if(reader != null)
							reader.close();
					} catch (IOException e) {
					}
				} 
			}
		}
		return evaluationContent;
	}

	public void setEvaluationContent(String evaluationContent) {
		this.evaluationContent = evaluationContent;
	}

	

	
}
